package ccc.virus.attend.service;

import ccc.virus.attend.dao.AttendMapper;
import ccc.virus.attend.entity.Attend;
import ccc.virus.attend.vo.QueryCondition;
import ccc.virus.common.page.PageQueryBean;
import ccc.virus.common.utils.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by JackWangon[www.aiprogram.top] 2017/6/20.
 */
@Service("attendServiceImpl")
public class AttendServiceImpl implements AttendService{

    /**
     * 中午十二点 判定上下午
     */
    private static final int NOON_HOUR = 12 ;
    private static final int NOON_MINUTE = 00 ;

    private Log log = LogFactory.getLog(AttendServiceImpl.class);

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Autowired
    private AttendMapper attendMapper;

    public void signAttend(Attend attend) throws Exception {
        try {
            Date today = new Date();
            attend.setAttendDate(today);
            attend.setAttendWeek((byte) DateUtils.getTodayWeek());
            //查询当天 此人有没有打卡记录
            Attend todayRecord=attendMapper.selectTodaySignRecord(attend.getUserId());
            Date noon = DateUtils.getDate(NOON_HOUR,NOON_MINUTE);
            if(todayRecord==null){
                //打卡记录还不存在
                if(today.compareTo(noon)<=0){
                    //打卡时间 早于12点 上午打卡
                    attend.setAttendMoring(today);
                }else {
                    //晚上打卡
                    attend.setAttendEvening(today);
                }
                attendMapper.insertSelective(attend);
            }else{
                if(today.compareTo(noon)<=0){
                    //打卡时间 早于12点 上午打卡
                    return;
                }else {
                    //晚上打卡
                    todayRecord.setAttendEvening(today);
                    attendMapper.updateByPrimaryKeySelective(todayRecord);
                }
            }

            //中午十二点之前打卡 都算早晨打卡  如果9.30以后 直接异常 算迟到
            // 十二点以后都算下午打卡
            // 下午打卡 检查与上午打卡时间差 18点之前  算异常
            // 不足八小时都算异常 并且 缺席多长时间 要存进去

        }catch (Exception e){
            log.error("用户签到异常",e);
            throw e;
        }

    }

    /**
    * @Author:CCC
    * @Description:考勤列表
    * @Date:2018/8/30 0030
    */
    public PageQueryBean listAttend(QueryCondition condition) {
        condition.setUserId(1l);
        condition.setStartDate(condition.getRangeDate().split("/")[0]);
        condition.setEndDate(condition.getRangeDate().split("/")[1]);

        int count = attendMapper.countByCondition(condition);
        PageQueryBean page = new PageQueryBean();
        if(count>0){
            page.setStartRow(count);
            page.setCurrentPage(condition.getCurrentPage());
            page.setPageSize(condition.getPageSize());
            List<Attend> attendList = attendMapper.selectAttendPage(condition);
            page.setItems(attendList);
        }
        return page;
    }

    /**
    * @Author:CCC
    * @Description:定时器校验考勤
    * @Date:2018/8/30 0030
    */
    public void checkAttend() {

    }
}
